using NUnit.Framework;
using Sandia.MVCFramework.Common;
using Sandia.MVCFramework.Data;
using Sandia.MVCFramework.View;
using Sandia.MVCFramework.ViewManager;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Sandia.MVCFramework.Test
{
    /// <summary>
    /// Tests for the ViewManager namespace.
    /// </summary>
    [TestFixture]
    public class ViewManagerTests : FailOnErrorLoggingTestFixture
    {
        /// <summary>
        /// A test for the StandardViewManagerState object.
        /// </summary>
        [Test]
        public void StandardViewManagerStateTest()
        {
            StandardViewManagerState state = new StandardViewManagerState();

            // Check the mainLayout tree getter/setter.
            ViewLayoutNode node = new ViewLayoutNode(null);
            state.MainLayout = node;
            Assert.AreSame(node, state.MainLayout, "Layout tree was not set correctly.");
        }

        /// <summary>
        /// A test for the ViewLayoutNode object.
        /// </summary>
        [Test]
        public void ViewLayoutNodeTest()
        {
            DummyView view1 = new DummyView(null, null);
            ViewLayoutNode node = new ViewLayoutNode(view1);
            // Test that the constructor set the view.
            Assert.AreSame(view1, node.View, "Constructor did not set the view correctly.");

            // Test the View getter/setter.
            DummyView view2 = new DummyView(null, null);
            node.View = view2;
            Assert.AreSame(view2, node.View, "View not set correctly.");

            // Test the parent getter/setter
            ViewLayoutNode parent = new ViewLayoutNode(view1);
            node.Parent = parent;
            Assert.AreSame(parent, node.Parent, "Parent not set correctly.");
        }

        /// <summary>
        /// A test for the SplitLayoutNode object.
        /// </summary>
        [Test]
        public void SplitLayoutNodeTest()
        {
            ViewLayoutNode leftChild = new ViewLayoutNode(new DummyView(null, null));
            ViewLayoutNode rightChild = new ViewLayoutNode(new DummyView(null, null));
            SplitLayoutNode node = new SplitLayoutNode(leftChild, rightChild, Orientation.Horizontal, 1);
            // Test the constructor settings.
            Assert.AreSame(leftChild, node.LeftChild, "Left child was not set correctly.");
            Assert.AreSame(rightChild, node.RightChild, "Right child was not set correctly.");
            Assert.AreEqual(Orientation.Horizontal, node.Orientation, "Orientaion was not set correctly.");
            Assert.AreEqual(1, node.SplitterDistance, "Splitter distance was not set correctly.");

            // Test the parent getter/setter
            ViewLayoutNode parent = new ViewLayoutNode(new DummyView(null, null));
            node.Parent = parent;
            Assert.AreSame(parent, node.Parent, "Parent not set correctly.");


            ViewLayoutNode child = new ViewLayoutNode(new DummyView(null, null));
            // Test the left child getter/setter.
            node.LeftChild = child;
            Assert.AreSame(child, node.LeftChild, "Left child was not set correctly.");

            // Test the right child getter/setter.
            node.RightChild = child;
            Assert.AreSame(child, node.RightChild, "Right child was not set correctly.");

            // Test the orientation getter/setter.
            node.Orientation = Orientation.Vertical;
            Assert.AreEqual(Orientation.Vertical, node.Orientation, "Orientation was not set correctly.");

            // Test the splitter distance getter/setter.
            node.SplitterDistance = 25;
            Assert.AreEqual(25, node.SplitterDistance, "Splitter distance was not set correctly.");
        }

        /// <summary>
        /// A test for the ViewToolStripButton object.
        /// </summary>
        [Test]
        public void ViewToolStripButtonTest()
        {
            ViewLayoutNode node1 = new ViewLayoutNode(new DummyView(null, null));
            ViewToolStripButton button = new ViewToolStripButton(node1);
            // Test the constructor setting.
            Assert.AreSame(node1, button.Node, "Node was not set correctly.");

            // Test the node getter/setter.
            ViewLayoutNode node2 = new ViewLayoutNode(new DummyView(null, null));
            button.Node = node2;
            Assert.AreSame(node2, button.Node, "Node was not set correctly.");
        }
    }

    /// <summary>
    /// A partial implementatiom of IView for use in testing only.
    /// </summary>
    class DummyView : AbstractStandardView
    {
        #region IView Members

        public DummyView(IRequestRouter requestRouter, IPublisher publisher)
            : base(requestRouter, publisher)
        {
        }

        public new bool Minimized
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        public new string Name
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public new Image ViewImage
        {
            get 
            {
                FileInfo info = new FileInfo(@"..\..\OrgChartHS.png");
                return Image.FromFile(info.FullName); 
            }
        }

        public new bool ToolBarVisible
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        #endregion

        #region ISubscriber Members

        public override IList<Type> DesiredSubscriptions
        {
            get
            {
                return new List<Type>();
            }
        }

        public override void HandlePublishedData(
            DataPacket dataPacket)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        protected override void setupView()
        {
        }

        public override void Start()
        {
            // Nothing to do.
        }     
    }
}
